package com.useeinfo.oa.open.admin.repair;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.repair.biz.RepairLevelBiz;
import com.useeinfo.oa.modules.repair.entity.RepairLevel;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: xudong.sun
 * @Date: 2019/3/4 10:53
 * @Description:
 */
@Controller
@RequestMapping("/web/admin/repairLevel")
public class RepairLevelAction {

    @Autowired
    private RepairLevelBiz repairLevelBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/repairLevel/list");
        return modelAndView;
    }

    /**
     * 查询列表
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "orderBy", required = false) String orderBy,
                               HttpServletRequest request
    ) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }

        if (StringUtils.isNotBlank(orderBy)) {
            queryParam.setSqlOrder("order by " + orderBy);
        }
        return repairLevelBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public ModelAndView editPage(String id) {

        ModelAndView modelAndView = new ModelAndView();
        RepairLevel repairLevel = repairLevelBiz.findModel(StringConverters.null2Long(id));
        modelAndView.addObject("repairLevel", repairLevel);
        modelAndView.setViewName("pages/admin/repairLevel/edit");
        return modelAndView;
    }


    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public ResultDto addOrUpdate(@RequestParam(required = false)String presence,@RequestParam(required = false) String finish,RepairLevel repairLevel, HttpServletRequest request
    ) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (enterprise == null) {
            return ResultUtil.error("登陆过期，请重新登录");
        }
        if(repairLevel.getPresenceTime() > repairLevel.getFinishTime()){
            return ResultUtil.error("需求到场时间不能大于需求完成时间");
        }
        repairLevel.setEnterprise(enterprise);
        if(StringUtils.isNotBlank(presence)){
            repairLevel.setPresenceTime(StringConverters.null2Int(presence));
        }
        if(StringUtils.isNotBlank(finish)){
            repairLevel.setFinishTime(StringConverters.null2Int(finish));
        }
        repairLevelBiz.addOrUpdate(repairLevel);
        return ResultUtil.success();
    }

}
